package com.gsm.phimhanhdong;

import java.util.ArrayList;

import com.google.gson.internal.bind.SqlDateTypeAdapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.media.MediaMetadataRetriever;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "phimhanhdong2013";

	private static final int DATABASE_VERSION = 1;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	private static DatabaseHelper mInstance = null;

	public synchronized static DatabaseHelper getInstance(Context ctx) {

		// Use the application context, which will ensure that you
		// don't accidentally leak an Activity's context.
		// See this article for more information:
		if (mInstance == null) {
			mInstance = new DatabaseHelper(ctx.getApplicationContext());
		}
		return mInstance;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		createDatabaseAndTables(db);
	}

	public boolean isOpen() {
		SQLiteDatabase db = getReadableDatabase();
		if (db.isOpen()) {
			db.close();
			return true;
		}
		return false;
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("drop table if exist " + PlaylistDetailTable.NAME);
		onCreate(db);
	}

	public void createDatabaseAndTables(SQLiteDatabase db) {
		PlaylistDetailTable.createTable(db);
	}

	public static class PlaylistDetailTable {
		public static final String NAME = "playlist_detai";

		public static void createTable(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE  " + NAME + "(" + Column.ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT , "
					+ Column.PLAYLIST_ID + " TEXT, " + Column.PLAYLIST_COUNT
					+ " TEXT " + ");");
		}

		public class Column {
			public static final String ID = "_id";

			public static final String PLAYLIST_ID = "playlist_id";

			public static final String PLAYLIST_COUNT = "playlist_count";

		}
	}

	public boolean checkPlaylistAvailable(String playlist_id) {
		SQLiteDatabase db = getReadableDatabase();
		Cursor c = db.rawQuery("select * from " + PlaylistDetailTable.NAME
				+ " where " + PlaylistDetailTable.Column.PLAYLIST_ID + "='"
				+ playlist_id + "'", null);
		if (c.getCount() == 0) {
			c.close();
			db.close();
			return false;
		}
		c.close();
		db.close();
		return true;
	}

	public void insertPlayList(String playlist_id, String playlist_count) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues vl = new ContentValues();
		vl.put(PlaylistDetailTable.Column.PLAYLIST_ID, playlist_id);
		vl.put(PlaylistDetailTable.Column.PLAYLIST_COUNT, playlist_count);
		db.insert(PlaylistDetailTable.NAME, null, vl);
		db.close();
	}

	public String getCountFromPlaylistId(String playlist_id) {
		SQLiteDatabase db = getReadableDatabase();
		Cursor c = db.rawQuery("select * from " + PlaylistDetailTable.NAME
				+ " where " + PlaylistDetailTable.Column.PLAYLIST_ID + "='"
				+ playlist_id + "'", null);
		if (c.getCount() > 0) {
			c.moveToFirst();
			String count = c
					.getString(c
							.getColumnIndexOrThrow(PlaylistDetailTable.Column.PLAYLIST_COUNT));
			db.close();
			c.close();
			return count;
		}
		return "0";
	}

	public void updatePlaylist(String playlist_id, String playlist_count) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues vl = new ContentValues();
		vl.put(PlaylistDetailTable.Column.PLAYLIST_COUNT, playlist_count);
		db.update(PlaylistDetailTable.NAME, vl,
				PlaylistDetailTable.Column.PLAYLIST_ID + "='" + playlist_id
						+ "'", null);
		db.close();
	}

	public Cursor getCursorTotalPlaylist() {
		SQLiteDatabase db = getReadableDatabase();
		Cursor c = db.rawQuery("select * from " + PlaylistDetailTable.NAME,
				null);
		return c;
	}

	public ArrayList<Playlist> getPlaylistOnlyForCheckId() {
		SQLiteDatabase db = getReadableDatabase();
		ArrayList<Playlist> playlists = new ArrayList<Playlist>();
		Cursor c = db.rawQuery("select * from " + PlaylistDetailTable.NAME,
				null);
		if (c.getCount() > 0) {
			c.moveToFirst();
			do {
				String id = c
						.getString(c
								.getColumnIndex(PlaylistDetailTable.Column.PLAYLIST_ID));
				Playlist playlist = new Playlist(id);
				playlists.add(playlist);
			} while (c.moveToNext());
		}
		db.close();
		c.close();
		return playlists;
	}

	public int getTotalCountPlayList() {
		SQLiteDatabase db = getReadableDatabase();
		Cursor c = db.rawQuery("select * from " + PlaylistDetailTable.NAME,
				null);
		int count = 0;
		if (c.getCount() > 0) {
			c.moveToFirst();
			do {
				count += Integer
						.valueOf(c.getString(c
								.getColumnIndex(PlaylistDetailTable.Column.PLAYLIST_COUNT)));
			} while (c.moveToNext());
		}
		db.close();
		c.close();
		return count;
	}

	public void deletePlaylist(String id) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = getWritableDatabase();
		db.delete(PlaylistDetailTable.NAME, PlaylistDetailTable.Column.ID+"='"+id+"'", null);
		db.close();
		
	}
}
